package com.xiaofengstu.mcams.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.zxing.WriterException;
import com.xiaofengstu.mcams.annotation.ILog;
import com.xiaofengstu.mcams.dto.AssetDTO;
import com.xiaofengstu.mcams.dto.BasicResultDTO;
import com.xiaofengstu.mcams.enums.RespStatusEnum;
import com.xiaofengstu.mcams.web.entity.TAsset;
import com.xiaofengstu.mcams.web.entity.Workflow;
import com.xiaofengstu.mcams.web.service.TAssetService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.datetime.joda.LocalDateTimeParser;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@RestController
@RequestMapping("/web/asset")
@RequiredArgsConstructor
public class TAssetController {

  private final TAssetService assetService;


  @ILog("获取资产列表")
  @GetMapping("/list")
  public BasicResultDTO<Page<AssetDTO>> getAssets(Integer page, Integer size) {
    Page<AssetDTO> ipage = new Page<>(page, size);
    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.getAssetList(ipage));
  }

  @ILog("新增资产")
  @PostMapping("/add")
  public BasicResultDTO<TAsset> add(@RequestBody TAsset asset) {

    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.add(asset));
  }


  @ILog("更新资产信息")
  @PostMapping("/update")
  public BasicResultDTO<Boolean> update(@RequestBody TAsset asset) {
    asset.setInDate(LocalDateTime.now());
    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.updateById(asset));
  }


  @ILog("资产申购")
  @PostMapping("/buyApply")
  public BasicResultDTO<Boolean> buyApply(@RequestBody TAsset asset) {
    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.buyApply(asset));
  }


  @ILog("生成资产二维码")
  @PostMapping("/generQR")
  public BasicResultDTO<String> generQR(@RequestBody TAsset asset) throws IOException, WriterException {
    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.generQRCode(asset));
  }


  @ILog("报表查询资产数量")
  @GetMapping("/getDate")
  public BasicResultDTO<?> getDates() {
    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.getDates());
  }

  @ILog("报表查询资产价值")
  @GetMapping("/getValues")
  public BasicResultDTO<?> getValues() {
    return new BasicResultDTO<>(RespStatusEnum.SUCCESS, assetService.getValues());
  }


}
